package lk.util;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;

import java.io.IOException;

/**
 * FTP 对象池
 *
 */
public class FtpClientPool {

    public static GenericObjectPool<FTPClient> FTP_CLIENT_POOL;

    private FtpClientPool() {}

    public static FTPClient borrowObject() throws Exception {
        return FTP_CLIENT_POOL.borrowObject();
    }

    public static void returnObject(FTPClient ftpClient) {
        try {
            ftpClient.changeWorkingDirectory("/");
        }catch (IOException e){
            System.out.println("归还 FTP 对象时切换到目录失败:" + FtpUtil.FTP_BASE_PATH );
            e.printStackTrace();
        }
        FTP_CLIENT_POOL.returnObject(ftpClient);
    }
}